from fastapi import APIRouter, Query, Depends

from application.utils import get_rich_logger
from authentication.jwt import get_current_user
from domain.artifact import Artifact, ArtifactInDB
from domain.authentication import User
from artifact.artifacts import add_new_artifact_record, view_artifact_information, update_artifact_metadata_record, \
    delete_artifact_record

logger = get_rich_logger()

artifact_router = APIRouter(
    prefix="/artifact",
    tags=['artifact']
)


@artifact_router.post("/create")
async def create_new_artifact_meta(
        artifact_meta: Artifact,
        current_user: User = Depends(get_current_user)
):
    artifact_in_db = ArtifactInDB(
        name=artifact_meta.name,
        uuid=current_user.uuid.hex,
        artifact_info=artifact_meta.json()
    )
    logger.debug(f"Artifact metadata: {artifact_in_db}")
    new_record_response = await add_new_artifact_record(
        new_record=artifact_in_db,
        user=current_user
    )
    return new_record_response


@artifact_router.get("/fetch")
async def get_artifact_metadata(
        artifact_id: str | None = Query(
            default=None,
            alias="artifact-id",
            title="Target artifact artifact record id.",
            description="The artifact artifact metadata intended to get information about.",
        ),
        current_user: User = Depends(get_current_user)
):
    metadata_record = await view_artifact_information(
        target_record_id=artifact_id,
        user=current_user
    )
    return metadata_record


@artifact_router.patch("/update")
async def update_file_system_config(
        new_artifact_meta: Artifact,
        artifact_id: str = Query(
            ...,
            alias="artifact-id",
            title="Target artifact artifact record id.",
            description="The artifact artifact metadata intended to update.",
        ),
        current_user: User = Depends(get_current_user)
):
    updated_record = await update_artifact_metadata_record(
        user=current_user,
        update_record_id=artifact_id,
        new_metadata=new_artifact_meta
    )
    return updated_record


@artifact_router.delete("/delete")
async def delete_file_system(
        artifact_id: str = Query(
            ...,
            alias="artifact-id",
            title="Target artifact artifact record id.",
            description="The artifact artifact metadata intended to be deleted.",
        ),
        current_user: User = Depends(get_current_user)
):
    ret = await delete_artifact_record(
        target_artifact_id=artifact_id,
        user=current_user
    )
    return ret
